package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.domain.User;
import com.example.service.UserService;
import com.example.service.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService = new UserServiceImpl();

    @ResponseBody
    @RequestMapping(value = "selectUser")
    public List<User> selectUser(@RequestParam("search") String search, HttpServletResponse response) throws Exception {
        List<User> userList = userService.selectUser(search);
        return userList;
    }

    @ResponseBody
    @RequestMapping(value = "searchUserByAll")
    public void searchUserByAll(@RequestParam("uid") Integer id,
                                @RequestParam("uname") String name,
                                HttpServletResponse response) {
        try {
            List<User> userList = userService.searchUserByAll(id,name);
            JSONArray data = JSONArray.parseArray(JSON.toJSONString(userList));
            response.setCharacterEncoding("utf-8");
            PrintWriter printWriter =response.getWriter();
            System.out.println(data);
            printWriter.append(data.toString());

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "showAllUser")
    public void showAllUser(HttpServletResponse response){
        try{
            List<User> userList = userService.showAllUser();
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(userList));
            response.setCharacterEncoding("utf-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.append(jsonArray.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("database_message")
    public List<String> database_message(){
        List<String> nameList=userService.selectUname();
        return nameList;
    }

    //=====================针对管理员的crud操作=======================

    @ResponseBody
    @RequestMapping("addManager")
    public List<User> addManager(@RequestParam("user") String user){
        Integer id = userService.selectIdByUname(user);
        userService.addManager(id);
        return userService.showManager();
    }

    @ResponseBody
    @RequestMapping("removeManager")
    public List<User> removeManager(@RequestParam("uid")Integer id){
        System.out.println(id);
        userService.removeManger(id);
        return userService.showManager();
    }

    @ResponseBody
    @RequestMapping("showManager")
    public List<User> showManager(){
        List<User> userList = userService.showManager();
        return userList;
    }
}
